﻿using System;
using System.IO;
using System.Net;

namespace ASPNETVN.PORTAL.Components.Helpers
{
    public class HtmlHelper
    {
        public static string GetHtmlFromUrl(string url)
        {
            HttpWebRequest req;
            HttpWebResponse res = null;
            string html = string.Empty;
            try
            {
                req = (WebRequest.Create(url) as HttpWebRequest);
                if (req != null)
                {
                    req.AllowAutoRedirect = true;
                    req.Method = "GET";
                    res = req.GetResponse() as HttpWebResponse;
                }
                if (res != null)
                    using (Stream s = res.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            html = sr.ReadToEnd();
                        }
                    }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
            }

            return html;
        }
    }
}
